from rest_framework.views import Response,APIView
from goods.models import Vegetable
from utils.jwt_tool import JwtTool


class AddVegetable(APIView):
    def post(self,request):
        token = request.headers.get("token")
        j = JwtTool()
        payload = j.check_token(token)
        v_name = request.data.get("v_name")
        v_logo = request.data.get("v_logo")
        v_class_id = request.data.get("v_class_id")
        price = request.data.get("price")
        veg_text = request.data.get("veg_text")
        tasteinfo_name = request.data.get("tasteinfo_name")
        Vegetable.objects.create(v_name=v_name,
                                 v_logo=v_logo,
                                 v_class_id=v_class_id,
                                 price=price,
                                 veg_text=veg_text,
                                 tasteinfo_name=tasteinfo_name,
                                 m_id=payload['m_id'])
        return Response({"code":200, "msg":"添加菜品成功"})


class DelVegetable(APIView):
    def delete(self,request):
        id = request.data.get('id')
        Vegetable.objects.filter(id=id).delete()
        return Response({'msg':'删除菜品成功', 'code':200})


class GetVegetable(APIView):
    def get(self,request):
        token = request.headers.get('token')
        j = JwtTool()
        payload = j.check_token(token)
        vege_all = Vegetable.objects.filter(m_id=payload['m_id']).all()
        list3 = []
        for i in vege_all:
            data3 = {
                'id': i.id,
                "v_name":i.v_name,
                "v_logo":i.v_logo,
                "v_class_id":i.v_classify2_type.get(str(i.v_class_id)),
                "price":i.price,
                "veg_text":i.veg_text,
                "tasteinfo_name":i.tasteinfo_name
            }
            list3.append(data3)
        return Response({'msg':'菜品展示成功', 'code':200, 'data':list3})


class UpdateVegetable(APIView):
    def put(self,request):
        id = request.data.get('id')
        v_name = request.data.get("v_name")
        v_logo = request.data.get("v_logo")
        v_classify = request.data.get("v_classify")
        veg_text = request.data.get("veg_text")
        price = request.data.get("price")
        Vegetable.objects.filter(id=id).update(v_name=v_name,v_logo=v_logo,v_classify=v_classify,price=price,veg_text=veg_text)
        return Response({"code":200,"msg":"修改菜品信息成功"})